package org.proxycine.gestsalles.web.beans;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.proxycine.gestsalles.model.entities.Employe;
import org.proxycine.gestsalles.services.LoginService;
import org.proxycine.gestsalles.web.constantes.*;

import java.io.Serializable;
 
@ManagedBean
@SessionScoped
public class LoginBean implements Serializable {
	
	private static final long serialVersionUID = 1L;
	
	private String login;
	private String password;
	private String errorMsg;
	private boolean loginSucces;
	private boolean loginButtonPressed = false;
	private Employe employeLogged;
	private int accordionActiveIndex = 0;
	private int dateTabActiveIndex = 0;
	
	public String valideLogin() {
		employeLogged = LoginService.getInstance().valideLogin(login, password);
		loginButtonPressed = true;
		
		if(employeLogged != null) {
			setLoginSucces(true);
		}
		else {
			setLoginSucces(false);
		}
		
		return "login";
	}
	
	public String logout() {
		FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
		employeLogged = null;
		this.loginSucces = false;
		
		return "logout";
	}
	
	public String getLogin() {
		return login;
	}
	
	public void setLogin(String login) {
		this.login = login;
	}
	
	public String getPassword() {
		return password;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	
	public void setErrorMsg(String errorMsg) {
		this.errorMsg = errorMsg;
	}

	public String getErrorMsg() {
		if(!loginSucces && loginButtonPressed)
			errorMsg = ConstantesWeb.ERROR_MESSAGE_LOGIN;
		else
			errorMsg = "";
		
		return errorMsg;
	}

	public void setLoginSucces(boolean loginSucces) {
		this.loginSucces = loginSucces;
	}

	public boolean isLoginSucces() {
		return loginSucces;
	}
	
	public Employe getEmployeLogged() {
		return this.employeLogged;
	}
	
	public int getAccordionActiveIndex() {
		return this.accordionActiveIndex;
	}
	
	public void setAccordionActiveIndex(int index) {
		this.accordionActiveIndex = index;
	}

	public int getDateTabActiveIndex() {
		return dateTabActiveIndex;
	}

	public void setDateTabActiveIndex(int dateTabActiveIndex) {
		this.dateTabActiveIndex = dateTabActiveIndex;
	}
}
